﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;
using SmartSpace.Core;

namespace SmartSpace.Data
{
    public class DataContext : DomainContext
    {
        public DataContext(DbContext dbContext)
            : base(dbContext)
        {

        }

        public static void Build(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<DictionaryType>().ToTable("__DictionaryType");
            modelBuilder.Entity<DictionaryType>().HasKey(p => p.Id);
            modelBuilder.Entity<DictionaryType>().Property(p => p.Id).IsRequired();

            modelBuilder.Entity<DictionaryData>().ToTable("__DictionaryData");
            modelBuilder.Entity<DictionaryData>().HasKey(p => p.Id);
            modelBuilder.Entity<DictionaryData>().Property(p => p.Id).IsRequired();
            modelBuilder.Entity<DictionaryData>().HasRequired(p => p.Type)
                .WithMany(p => p.Datas).HasForeignKey(p => p.TypeId).WillCascadeOnDelete(false);
            modelBuilder.Entity<DictionaryData>().HasOptional(p=>p.Parent)
                .WithMany(p=>p.Children).HasForeignKey(p => p.ParentId);

            modelBuilder.Entity<TypeMetadata>().ToTable("__TypeMetadata");
            modelBuilder.Entity<TypeMetadata>().HasKey(p => p.Id);
            modelBuilder.Entity<TypeMetadata>().Property(p => p.Id).IsRequired();

            modelBuilder.Entity<EntityMetadata>().ToTable("__EntityMetadata");
            modelBuilder.Entity<EntityMetadata>().HasKey(p => p.Id);
            modelBuilder.Entity<EntityMetadata>()
                .HasMany<PropertyMetadata>(p => p.Properties)
                .WithRequired().HasForeignKey(p => p.EntityId);

            modelBuilder.Entity<EntityRelation>().ToTable("__EntityRelation");
            modelBuilder.Entity<EntityRelation>().HasKey(p => p.Id);
            modelBuilder.Entity<EntityRelation>()
                .HasRequired<EntityMetadata>(p => p.PrimaryEntity)
                .WithMany().HasForeignKey(p => p.PrimaryEntityId).WillCascadeOnDelete(false);
            modelBuilder.Entity<EntityRelation>()
                .HasRequired<EntityMetadata>(p => p.RelationEntity)
                .WithMany().HasForeignKey(p => p.RelationEntityId).WillCascadeOnDelete(false);

            modelBuilder.Entity<PropertyMetadata>().ToTable("__PropertyMetadata");
            modelBuilder.Entity<PropertyMetadata>().HasKey(p => p.Id);
            modelBuilder.Entity<PropertyMetadata>().Property(p => p.Id).IsRequired();
            modelBuilder.Entity<PropertyMetadata>()
                .HasRequired<TypeMetadata>(p => p.Type)
                .WithMany().HasForeignKey(p => p.TypeId);

            //modelBuilder.Entity<ViewMetadata>().ToTable("__ViewMetadata");
            //modelBuilder.Entity<ViewMetadata>().HasKey(p => p.Id);
            //modelBuilder.Entity<ViewMetadata>()
            //    .HasRequired<EntityMetadata>(p => p.Entity)
            //    .WithMany().HasForeignKey(p => p.EntityId).WillCascadeOnDelete(false);
            //modelBuilder.Entity<ViewMetadata>()
            //    .HasMany<FieldMetadata>(p => p.Fields)
            //    .WithRequired(p => p.ModuleView).HasForeignKey(p => p.ViewId).WillCascadeOnDelete(false);

            //modelBuilder.Entity<FieldMetadata>().ToTable("__FieldMetadata");
            //modelBuilder.Entity<FieldMetadata>().HasKey(p => p.Id);
            //modelBuilder.Entity<FieldMetadata>()
            //    .HasRequired<PropertyMetadata>(p => p.Property);
        }
    }
}
